#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "..//include//n_ary.h"
#include "..//include//createvfs.h"



Tree del_nary(Tree root, char filepath[]){
   
   char str[100];
   char newstr1[20];
   char newstr2[20]; 
   char node_label[50];  
   char abspath[100]=""; 

   int num;
   int j=0;
   int level=0;
   int count=1;
   int pos=1;
   int i; 
   int k=0;    
   
   Tree cur_tree;   
   Tree prev_tree;
   Tree sib=cur_tree;
   Tree new;
   

   NODE first=NULL;   
   NODE cur=first;
   NODE last;

   
   if(strcmp(filepath,"/")==0) return root;
   
      // Split the path based on '/'
	
     while(filepath[pos]!='\0'){
         strcat(abspath,"/");	
         j=0;
         num=split1(filepath,pos);
         
         if(num>(strlen(filepath)-1)) num = strlen(filepath)-1;
         
         for(i=pos; i<=num; i++){
            newstr2[j]=filepath[i];
            j++;
         }
         newstr2[j]='\0';     

	 /*k = 0;
	 for(i=0; i<=pos; i++){
		abspath[k] = filepath[i];
		k++;

	 }
	 abspath[k]='\0';*/
                 
         first=insert_rear_ll(newstr2,abspath,first);
         strcat(abspath,newstr2);
         if(num>=strlen(filepath)-1) break;
         pos=num+2;
        
      }

      last=cur;
      cur=first;
      while(cur->next!=NULL){
        count=count+1;
        cur=cur->next;
      }   
	
         
      
      //Actual deletion from the node

      cur=first;
      cur_tree=root->left;
      sib=root->left;
      prev_tree=root;     
      while(count > 0){	 // count = 1 => leaf node
      	//printf("\n\n Count : %d",count);
		if(cur_tree!=NULL){ 
		
        	if(strcmp(cur_tree->node_label,cur->entry) == 0 && count!=1){  // if (the two strings are equal) {...}
        		//printf("\n label match and count !=1");
            		cur=cur->next;
           		prev_tree=cur_tree;
			cur_tree=cur_tree->left;
			sib=cur_tree;
			//printf("\n sib value : %s", sib->node_label);		
			count=count-1;
       		}
       		else if(strcmp(cur_tree->node_label,cur->entry) == 0 && count==1){
       			//printf("\n label match and count =1");
       			count = count-1;
       		}
       		else {	// traverse to the right siblings
       			//printf("\n else");
       			//cur = cur -> next;
       			prev_tree = cur_tree;
       			cur_tree = cur_tree -> right;
       		}
       	}
       	else {
      
       			//printf("\n ERR_VFS_DELETEDIR_01 CANNOT_FIND_SPECIFIED_PATH_OR_DIR");
			//printf("\n ERR_VFS_REMOVEFILE_01 CANNOT_FIND_SPECIFIED_PATH_OR_FILE");
				return root;
       	}
      }
      
      /*// debug
      printf("\n Outside while: sib value : %s", sib->node_label);
      printf("\n Outside while: current value : %s", cur_tree->node_label);
      printf("\n Outside while: prev value : %s", prev_tree->node_label);*/
      
      
      
      // cur_tree is the node to be deleted.
      // checking if the directory is empty or not
      
      if (cur_tree -> left != NULL) {
      	//printf("\n ERR_VFS_DELETEDIR_02 DIRECTORY_IS_NOT_EMPTY");
      	return root;
      }
      else {
      	if (cur_tree == sib) {							// sib points to the leftmost node in that level
      		prev_tree -> left = cur_tree -> right;
      		free(cur_tree);
      		cur_tree -> right = NULL;
      	}
      	else {
      		prev_tree -> right = cur_tree -> right;
      		free(cur_tree);
      		cur_tree -> right = NULL;
      	}
      		
    }
    
	return root;
}

int split1(char *str,int pos){
   int j,i;
   j=0;
   for(i=pos; i<strlen(str);i++){
      if(str[i]=='/' || str[i]=='"') return i-1;
   }
   
   return i;
}

